home *** CD-ROM | disk | FTP | other *** search
/ Teach Yourself Web Publi…sional Reference Edition) / Teach Yourself Web Publishing HTML 3.2.iso / mac / iso9660 / SOURCE / CHAP25 / TICKJAVA.ZIP / Ticker.java < prev    next >
Encoding:
Java Source  |  1996-04-26  |  2.1 KB  |  79 lines

  1. /* Exercise - Ticker.class */
  2. import java.applet.*;
  3. import java.awt.* ;
  4.  
  5. public class Ticker extends Applet implements Runnable {
  6.     Thread tkthread = null;
  7.     String tktext = "Exercise - ticker tape";
  8.     int tkspd = 1;
  9.     String tkfname = "TimesRoman";
  10.     int tkfsz = 12;
  11.     Font tkfont = null;
  12.     String tkdirection = "Left";
  13.     Dimension tksize = null;
  14.     int tktextwth = 0;
  15.     int tktexthgt = 0;
  16.     int tkpos = -63000;
  17.  
  18. public void init() {
  19.     String getval = null;
  20.     getval = getParameter("tktext");
  21.     tktext = (getval == null ) ? tktext : getval;
  22.     getval = getParameter("tkspd");
  23.     tkspd = (getval == null ) ? tkspd : (Integer.valueOf(getval).intValue());
  24.     getval = getParameter("tkfname");
  25.     tkfname = (getval == null) ? tkfname : getval ;
  26.     getval = getParameter("tkfsz");
  27.     tkfsz = (getval == null ) ? tkfsz : (Integer.valueOf(getval).intValue());
  28.     tkfont = new java.awt.Font( tkfname, Font.PLAIN, tkfsz ) ;
  29.     getval = getParameter("tkreverse");
  30.  
  31.     tkdirection = "Left";
  32.     tkpos  =  -63000 ;
  33.  
  34.     if ( getval != null ) {
  35.        if ( getval.equalsIgnoreCase( "Yes") ) {
  36.           tkdirection = "Right";
  37.           tkpos  = 63000;
  38.           }
  39.        }
  40.  
  41.     this.setBackground( Color.white );
  42.     }
  43.  
  44. public void start() {
  45.     tkthread = new Thread(this);
  46.     tkthread.start();
  47.     }
  48.  
  49. public void stop() {
  50.     tkthread.stop();
  51.     }
  52.  
  53. public void run() {
  54.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  55.     while (true) {
  56.       try {Thread.sleep( 10 ); } catch (InterruptedException e){}
  57.       repaint();
  58.       }
  59.     }
  60.  
  61. public void paint(Graphics tk) {
  62.     tksize = size();
  63.     tk.setFont(tkfont);
  64.     FontMetrics tkfm = tk.getFontMetrics();
  65.     tktexthgt = ( tktexthgt==0 ) ? tkfm.getHeight() : tktexthgt;
  66.     tktextwth = ( tktextwth==0 ) ? tkfm.stringWidth( tktext ) : tktextwth;
  67.     if (tkdirection=="Left") {
  68.        tkpos = ( tkpos <= tktextwth * -1 ) ? tksize.width : tkpos - tkspd;
  69.     }
  70.     else{
  71.        tkpos = ( tkpos > tksize.width ) ? 0 - tktextwth: tkpos + tkspd;
  72.     }
  73.     tk.setColor(Color.black);
  74.     tk.drawString( tktext, tkpos, ( tksize.height + tktexthgt ) / 2 );
  75.     }
  76. }
  77.  
  78.  
  79.